#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

import re
from pymongo import MongoClient

def GetExactName(name):
    regex = "\W(.*)\W"
    pattern = re.compile(regex, re.I)
    m = re.search(pattern, name)
    if m:
        return m.group(1)
    else:
        return m

def Search_CaseSensitive_Return(regex, line):
    regex2 = "(" + regex + ")"
    pattern = re.compile(regex2)
    m = re.search(pattern, line)
    if m:
        return m.group(1)
    else:
        return m


def Search_CaseIgnore_Return(regex, line):
    regex2 = "(" + regex + ")"
    pattern = re.compile(regex2, re.I)
    m = re.search(pattern, line)
    if m:
        return m.group(1)
    else:
        return m


def Findall_CaseIgnore_Return(regex, line):
    regex2 = "(" + regex + ")"
    pattern = re.compile(regex2, re.I)
    m = re.findall(pattern, line)
    return m


def Initialize_Province_Regex(filepath):
    infile = open(filepath, "r")
    province_regex = ""
    province_ht = {}
    for line in infile:
        line = line.rstrip()
        splitline = line.split("\t")
        province = splitline[1]
        province_regex += "\W" + province + "\W|"
        province_ht[province.upper()] = province
        if len(splitline) > 2:
            splitprovince = splitline[2].split("|")
            for item in splitprovince:
                province_regex += "\W" + item + "\W|"
                province_ht[item.upper()] = province
    infile.close()
    province_regex = province_regex[:-1].replace(".", "\.")

    return province_regex, province_ht


def Initialize_City_Regex(filepath_unique, filepath_confuse):
    infile_unique = open(filepath_unique, "r")
    city_regex_unique = ""
    province_split_city_regex = {}
    city_ht_unique = {}
    city2province_ht = {}
    for line in infile_unique:
        line = line.rstrip()
        splitline = line.split("\t")
        province = splitline[1]
        city = splitline[3]
        city_regex_unique += "\W" + city + "\W|"
        if province_split_city_regex.has_key(province):
            province_split_city_regex[province] += "\W" + city + "\W|"
        else:
            province_split_city_regex[province] = "\W" + city + "\W|"
        city_ht_unique[city.upper()] = city
        city2province_ht[city] = province
        if len(splitline) > 4:
            splitcity = splitline[4].split("|")
            for item in splitcity:
                city_regex_unique += "\W" + item + "\W|"
                province_split_city_regex[province] += "\W" + item + "\W|"
                city_ht_unique[item.upper()] = city
    infile_unique.close()

    infile_confuse = open(filepath_confuse, "r")
    city_regex_confuse = ""
    city_ht_confuse = {}
    temp = {}
    for line in infile_confuse:
        line = line.rstrip()
        splitline = line.split("\t")
        province = splitline[1]
        city = splitline[3]
        province_split_city_regex[province] += "\W" + item + "\W|"
        city_ht_unique[city.upper()] = city
        temp[city] = 0
    infile_confuse.close()
    for key in temp.keys():
        city_regex_confuse += "\W" + key + "\W|"
        city_ht_confuse[key.upper()] = key

    city_regex_unique = city_regex_unique[:-1].replace(".", "\.")
    city_regex_confuse = city_regex_confuse[:-1].replace(".", "\.")
    for key in province_split_city_regex.keys():
        if province_split_city_regex[key] != "":
            province_split_city_regex[key] = province_split_city_regex[key][:-1].replace(".", "\.")

    return city_regex_unique, city_ht_unique, city_regex_confuse, city_ht_confuse, province_split_city_regex, city2province_ht


def RecognizeChineseCity():
    import sys
    default_encoding = 'utf-8'
    if sys.getdefaultencoding() != default_encoding:
        reload(sys)
        sys.setdefaultencoding(default_encoding)

    path_parent = ""
    province_regex, province_ht = Initialize_Province_Regex(path_parent + "province.txt")
    city_regex_unique, city_ht_unique, city_regex_confuse, city_ht_confuse, province_split_city_regex, city2province_ht = Initialize_City_Regex(
            path_parent + "city_unique.txt", path_parent + "city_confuse.txt")

    DB_NAME = "organization"
    COLLECTION_NAME = "organization"

    connection = MongoClient('localhost', 27017)
    db = connection[DB_NAME]
    collection = db[COLLECTION_NAME]

    regex_label = "<.*?>"
    regex_sup = "<sup>.*?</sup>"
    regex_blank = " +"

    p_c_1 = 0
    p_c_2 = 0
    only_p = 0
    only_c = 0
    no_p_c = 0

    results = collection.find({"country": "China"})
    for result in results:
        org = result["org"]
        id = result["id"]
        org2 = re.sub(regex_sup, "", org, 0, re.I)
        org2 = re.sub(regex_label, "", org2, 0, re.I)
        org2 = " " + org2 + " "
        org2 = re.sub(regex_blank, " ", org2, 0, re.I)

        province = ""
        city = ""
        # Search for province first.
        search_province = Search_CaseIgnore_Return(province_regex, org2)
        if search_province != None:
            # If find province for the first search, search for city according to province.
            province = province_ht[GetExactName(search_province).upper()]
            yes_province_search_city = Search_CaseIgnore_Return(province_split_city_regex[province], org2)
            if yes_province_search_city != None:
                # Find city, record P&C
                raw_city = GetExactName(yes_province_search_city).upper()
                if city_ht_unique.has_key(raw_city):
                    city = city_ht_unique[raw_city]
                else:
                    city = "Xian"
                # city = city_ht_unique[GetExactName(yes_province_search_city).upper()]
                p_c_1 += 1
            else:
                # Cannot find city record P
                only_p += 1
        else:
            # If cannot find province for the first search, search for city in city unique list.
            no_province_search_unique_city = Search_CaseIgnore_Return(city_regex_unique, org2)
            if no_province_search_unique_city != None:
                # Find unique city, record P&C
                raw_city = GetExactName(no_province_search_unique_city).upper()
                if city_ht_unique.has_key(raw_city):
                    city = city_ht_unique[raw_city]
                else:
                    city = "Xian"
                # city = city_ht_unique[GetExactName(no_province_search_unique_city).upper()]
                province = city2province_ht[city]
                p_c_2 += 1
            else:
                # Cannot find unique city, search for confuse city.
                no_unique_city_search_confuse_city = Search_CaseIgnore_Return(city_regex_confuse, org2)
                if no_unique_city_search_confuse_city != None:
                    # Find confuse city, record C
                    city = city_ht_confuse[GetExactName(no_unique_city_search_confuse_city).upper()]
                    only_c += 1
                else:
                    no_p_c += 1
                    print org
        dataset = {}
        if province != "":
            dataset["province"] = province
        if city != "":
            dataset["city"] = city
        if len(dataset) != 0:
            collection.update_one({"id": id}, {"$set": dataset})
    connection.close()

    # print p_c_1
    # print p_c_2
    # print only_p
    # print only_c
    # print no_p_c


if __name__ == "__main__":
    RecognizeChineseCity()
